home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / JPNL Libraries / FTPClient.java < prev    next >
Text File  |  1996-05-23  |  3KB  |  128 lines

  1. package au.com.peter.libraries;
  2.  
  3. import au.com.peter.libraries.URL;
  4. import java.io.*;
  5. import java.net.*;
  6.  
  7. public class FTPClient extends NetClient {
  8.  
  9.     public void OpenFTPConnection( URL url ) throws IOException {
  10.         OpenConnection( url.host, url.port );        
  11.         Login( url.username, url.password );
  12.         if ( url.path != null && url.path.length() > 0 ) {
  13.             CD( url.path );
  14.         }
  15.     }
  16.     
  17.     public void DoOpenConnection( Socket conn ) throws IOException {
  18.         super.DoOpenConnection( conn );
  19.         if ( ReadResponseHighCode() != 2 ) {
  20.             throw new FTPClientException( "Connection refused by server" );
  21.         }
  22.     }
  23.     
  24.     public void Login( String username, String password ) throws IOException {
  25.         Login( username, password, null );
  26.     }
  27.     
  28.     public void Login( String username, String password, String account ) throws IOException {
  29.         CheckOpen();
  30.         int response;
  31.         
  32.         response = SendCommandHighCode( "USER " + username );
  33.         if ( response == 3 ) {
  34.             response = SendCommandHighCode( "PASS " + password );
  35.         }
  36.         if ( response == 3 ) {
  37.             response = SendCommandHighCode( "ACCT " + account );
  38.         }
  39.         if ( response != 2 ) {
  40.             throw new FTPClientException( "login failed" );
  41.         }
  42.     }
  43.     
  44.     public void CD( String path ) throws IOException {
  45.         SendCommandCheck( "CWD " + path );
  46.     }
  47.     
  48.     public void SetBinaryMode( boolean binary ) throws IOException {
  49.         if ( binary ) {
  50.             SendCommandCheck( "TYPE I" );
  51.         } else {
  52.             SendCommandCheck( "TYPE A" );
  53.         }
  54.     }
  55.     
  56.     public void SetMacBinaryMode( boolean macbinary ) throws IOException {
  57.         if ( macbinary ) {
  58.             SendCommandCheck( "MACB E" );
  59.         } else {
  60.             SendCommandCheck( "MACB D" );
  61.         }
  62.     }
  63.     
  64.     public Socket OpenDataConnection( String command ) throws IOException {
  65.         Socket data_connection = null;
  66.         ServerSocket data_listener  = new ServerSocket( 0, 1 );
  67.         try {
  68.             byte[] address;
  69.             if ( true ) {
  70.                 address = new byte[4];
  71.                 address[0] = (byte) 206;
  72.                 address[1] = (byte) 126;
  73.                 address[2] = (byte) 100;
  74.                 address[3] = (byte) 110;
  75.             } else {
  76.                 address = data_listener.getInetAddress().getAddress();
  77.             }
  78.             if ( address.length != 4 ) {
  79.                 throw new FTPClientException( "IP address must be four bytes" );
  80.             }
  81.             int port = data_listener.getLocalPort();
  82.  
  83.             String port_command = "PORT " +
  84.                 (address[0] & 0xFF) + "," +
  85.                 (address[1] & 0xFF) + "," +
  86.                 (address[2] & 0xFF) + "," +
  87.                 (address[3] & 0xFF) + "," +
  88.                 ((port >> 8) & 0xFF) + "," +
  89.                 (port & 0xFF);
  90.             SendCommandCheck( port_command );
  91.             
  92.             if ( SendCommandHighCode( command ) != 1 ) {
  93.                 throw new NetClientException( "command '"+ ObscureCommand( command ) + "' failed to return 1xx" );
  94.             }
  95.             
  96.             data_connection = data_listener.accept();
  97.         } finally {
  98.             data_listener.close();
  99.         }
  100.         
  101.         return data_connection;
  102.     }
  103.     
  104.     public InputStream OpenInputDataConnection( String command ) throws IOException {
  105.         return OpenDataConnection( command ).getInputStream();
  106.     }
  107.     
  108.     public OutputStream OpenOutputDataConnection( String command ) throws IOException {
  109.         return OpenDataConnection( command ).getOutputStream();
  110.     }
  111.     
  112.     public void CloseConnection() {
  113.         if ( ConnectionIsOpen() ) {
  114.             try {
  115.                 SendCommandCheck( "QUIT" );
  116.             } catch ( Exception e );
  117.             super.CloseConnection();
  118.         }
  119.     }
  120.     
  121. }
  122.  
  123. class FTPClientException extends NetClientException {
  124.     public FTPClientException( String s ) {
  125.         super( s );
  126.     }
  127. }
  128.